/*
 *  Copyright 2009 David Kuhn <david.s.kuhn at gmail.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.google.code.teamtools.client.widget;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.code.teamtools.client.i18n.AppTextConstants;
import com.google.code.teamtools.client.i18n.AppTextConstantsAdapter;
import com.google.gwt.user.client.Timer;

/**
 * 
 * @author David Kuhn <david.s.kuhn at gmail.com>
 */
public class LoginDialog extends Dialog
{
    /*
     * Default widget text.
     */
    protected static final AppTextConstants DEFAULT_TEXT = new AppTextConstantsAdapter()
    {
        @Override
        public String loginDialogPasswordFieldLabel()
        {
            return "Password";
        }

        @Override
        public String loginDialogTitle()
        {
            return "Please Login";
        }

        @Override
        public String loginDialogUsernameFieldLabel()
        {
            return "Username";
        }

        @Override
        public String loginDialogBusyText()
        {
            return "please wait...";
        }

        @Override
        public String loginDialogLoginButtonText()
        {
            return "Login";
        }

        @Override
        public String loginDialogResetButtonText()
        {
            return "Reset";
        }
    };

    protected AppTextConstants text;
    protected TextField<String> userName;
    protected TextField<String> password;
    protected Button reset;
    protected Button login;
    protected Status status;

    public LoginDialog()
    {
        this(DEFAULT_TEXT);
    }

    public LoginDialog(AppTextConstants text)
    {
        this.text = text;

        FormLayout layout = new FormLayout();
        layout.setLabelWidth(90);
        layout.setDefaultWidth(155);
        setLayout(layout);

        setButtonAlign(HorizontalAlignment.LEFT);
        setButtons("");
        setIcon(IconHelper.createStyle("user"));
        setHeading(this.text.loginDialogTitle());
        setModal(true);
        setBodyBorder(true);
        setBodyStyle("padding: 8px;background: none");
        setWidth(300);
        setResizable(false);

        KeyListener keyListener = new KeyListener()
        {
            @Override
            public void componentKeyUp(ComponentEvent event)
            {
                validate();
            }
        };

        userName = new TextField<String>();
        userName.setMinLength(4);
        userName.setFieldLabel(this.text.loginDialogUsernameFieldLabel());
        userName.addKeyListener(keyListener);
        add(userName);

        password = new TextField<String>();
        password.setMinLength(4);
        password.setPassword(true);
        password.setFieldLabel(this.text.loginDialogPasswordFieldLabel());
        password.addKeyListener(keyListener);
        add(password);

        setFocusWidget(userName);

        status = new Status();
        status.setBusy(this.text.loginDialogBusyText());
        status.hide();
        status.setAutoWidth(true);
        getButtonBar().add(status);

        getButtonBar().add(new FillToolItem());

        reset = new Button(this.text.loginDialogResetButtonText());
        reset.addSelectionListener(new SelectionListener<ButtonEvent>()
        {
            public void componentSelected(ButtonEvent ce)
            {
                userName.reset();
                password.reset();
                validate();
                userName.focus();
            }
        });

        login = new Button(this.text.loginDialogLoginButtonText());
        login.disable();
        login.addSelectionListener(new SelectionListener<ButtonEvent>()
        {
            public void componentSelected(ButtonEvent ce)
            {
                onSubmit();
            }
        });

        addButton(reset);
        addButton(login);
    }

    protected void onSubmit()
    {
        status.show();
        getButtonBar().disable();
        Timer t = new Timer()
        {
            @Override
            public void run()
            {
                LoginDialog.this.hide();
            }
        };
        t.schedule(2000);
    }

    protected boolean hasValue(TextField<String> field)
    {
        return field.getValue() != null && field.getValue().length() > 0;
    }

    protected void validate()
    {
        login.setEnabled(hasValue(userName) && hasValue(password) && password.getValue().length() > 3);
    }
}
